/**
 * 作者: wgx
 * 日期: 2024-12-03 15:45:50
 * 备注: 用户物资争夺模块的伙伴上场数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSnatchPartner;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserSnatchPartnerModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.partner_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库ID
    protected long userId; // 用户ID
    protected int partnerId; // partner_id
    protected int matchFightNum; // 匹配战斗的次数 (一次最多3次,3次后就进入cd时间)
    protected long resetTime; // 每日的重置时间
    protected long matchFightCdTime; // 匹配战斗的次数的cd开始时间 (一次最多3次,3次后就进入cd时间)
    protected int matchFightRecoverNum; // 匹配战斗次数的恢复次数 (一天最多恢复2次)
    protected int challengeNum; // 每日的挑战次数 (每天重置)

    public void reset() {
    }

    protected String getKey() {
        return "UserSnatchPartner." + userId + "." + partnerId;
    }

    protected String getListKey() {
        return "UserSnatchPartner." + userId;
    }

    protected String getListItemKey() {
        return partnerId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_snatch_partner"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库ID
        obj.put("userId", userId); // 用户ID
        obj.put("partnerId", partnerId); // partner_id
        obj.put("matchFightNum", matchFightNum); // 匹配战斗的次数 (一次最多3次,3次后就进入cd时间)
        obj.put("resetTime", resetTime); // 每日的重置时间
        obj.put("matchFightCdTime", matchFightCdTime); // 匹配战斗的次数的cd开始时间 (一次最多3次,3次后就进入cd时间)
        obj.put("matchFightRecoverNum", matchFightRecoverNum); // 匹配战斗次数的恢复次数 (一天最多恢复2次)
        obj.put("challengeNum", challengeNum); // 每日的挑战次数 (每天重置)
        return obj;
    }

    public PBUserSnatchPartner.Builder takeInitBuilder() {
        PBUserSnatchPartner.Builder sb = PBUserSnatchPartner.newBuilder();
        if (partnerId != 0) {
            sb.setPartnerId(partnerId);
        }
        if (matchFightNum != 0) {
            sb.setMatchFightNum(matchFightNum);
        }
        if (matchFightCdTime != 0) {
            sb.setMatchFightCdTime(matchFightCdTime);
        }
        if (matchFightRecoverNum != 0) {
            sb.setMatchFightRecoverNum(matchFightRecoverNum);
        }
        if (challengeNum != 0) {
            sb.setChallengeNum(challengeNum);
        }
        return sb;
    }

    public PBUserSnatchPartner.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserSnatchPartner.Builder sb = PBUserSnatchPartner.newBuilder();
        if (isFieldUpdate("partnerId")) {
            sb.setPartnerId(partnerId);
        }
        if (isFieldUpdate("matchFightNum")) {
            sb.setMatchFightNum(matchFightNum);
        }
        if (isFieldUpdate("matchFightCdTime")) {
            sb.setMatchFightCdTime(matchFightCdTime);
        }
        if (isFieldUpdate("matchFightRecoverNum")) {
            sb.setMatchFightRecoverNum(matchFightRecoverNum);
        }
        if (isFieldUpdate("challengeNum")) {
            sb.setChallengeNum(challengeNum);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getPartnerId() {
        return partnerId;
    }

    public void putPartnerId(int partnerId) {
        if (this.partnerId != partnerId) {
            this.partnerId = partnerId;
            updateRedisHashField(getKey(), "partnerId", this.partnerId);
        }
    }

    public void addPartnerId(int value) {
        this.partnerId += value;
        updateRedisHashField(getKey(), "partnerId", this.partnerId);
    }

    public int getMatchFightNum() {
        return matchFightNum;
    }

    public void putMatchFightNum(int matchFightNum) {
        if (this.matchFightNum != matchFightNum) {
            this.matchFightNum = matchFightNum;
            updateRedisHashField(getKey(), "matchFightNum", this.matchFightNum);
        }
    }

    public void addMatchFightNum(int value) {
        this.matchFightNum += value;
        updateRedisHashField(getKey(), "matchFightNum", this.matchFightNum);
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public long getMatchFightCdTime() {
        return matchFightCdTime;
    }

    public void putMatchFightCdTime(long matchFightCdTime) {
        if (this.matchFightCdTime != matchFightCdTime) {
            this.matchFightCdTime = matchFightCdTime;
            updateRedisHashField(getKey(), "matchFightCdTime", this.matchFightCdTime);
        }
    }

    public void addMatchFightCdTime(long value) {
        this.matchFightCdTime += value;
        updateRedisHashField(getKey(), "matchFightCdTime", this.matchFightCdTime);
    }

    public int getMatchFightRecoverNum() {
        return matchFightRecoverNum;
    }

    public void putMatchFightRecoverNum(int matchFightRecoverNum) {
        if (this.matchFightRecoverNum != matchFightRecoverNum) {
            this.matchFightRecoverNum = matchFightRecoverNum;
            updateRedisHashField(getKey(), "matchFightRecoverNum", this.matchFightRecoverNum);
        }
    }

    public void addMatchFightRecoverNum(int value) {
        this.matchFightRecoverNum += value;
        updateRedisHashField(getKey(), "matchFightRecoverNum", this.matchFightRecoverNum);
    }

    public int getChallengeNum() {
        return challengeNum;
    }

    public void putChallengeNum(int challengeNum) {
        if (this.challengeNum != challengeNum) {
            this.challengeNum = challengeNum;
            updateRedisHashField(getKey(), "challengeNum", this.challengeNum);
        }
    }

    public void addChallengeNum(int value) {
        this.challengeNum += value;
        updateRedisHashField(getKey(), "challengeNum", this.challengeNum);
    }

}